package com.atguigu.gmall.activity.receiver;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.activity.mapper.SecKillGoodsMapper;
import com.atguigu.gmall.activity.pojo.SeckillOrder;
import com.atguigu.gmall.activity.pojo.UserRecode;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.model.activity.SeckillGoods;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
public class SeckillOrderAddReceiver {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SecKillGoodsMapper secKillGoodsMapper;

    /**
     * 监听排队消息进行秒杀下单
     */
    @RabbitListener(queues = "seckill_queue")
    public void addOrder(String msg){
        // 将消息的类型转换为排队信息
        UserRecode userRecode = JSONObject.parseObject(msg, UserRecode.class);
        String userId = userRecode.getUserId();
        // 校验用户是否有未支付的秒杀订单
        Object o = redisTemplate.boundHashOps(RedisConst.SECKILL_ORDERS).get(userId);
        if(null != o){
            // 修改排队状态
            userRecode.setStatus(3);
            // 更新用户的排队状态
            redisTemplate.boundValueOps(RedisConst.SECKILL_USER + userId).set(userRecode);
            return;
        }
        String time = userRecode.getTime();
        String goodsId = userRecode.getGoodsId();
        // 校验商品是否在redis中
        SeckillGoods goods = (SeckillGoods) redisTemplate.boundHashOps(RedisConst.SECKILL_GOODS + ":" + time).get(goodsId);
        if(null != goods){
            // 商品类型的转换
            Integer stockCount = goods.getStockCount();
            // 判断库存
            if(stockCount > 0){
                // 构建订单信息
                SeckillOrder seckillOrder = new SeckillOrder();
                seckillOrder.setId(UUID.randomUUID().toString().replace("-",""));
                seckillOrder.setGoodsId(goodsId);
                seckillOrder.setNum(userRecode.getNum());
                seckillOrder.setMoney(userRecode.getNum() * goods.getCostPrice().doubleValue() + "");
                seckillOrder.setUserId(userId);
                seckillOrder.setCreateTime(userRecode.getCreateTime());
                seckillOrder.setStatus("0");

                // 将订单信息存入redis
                redisTemplate.boundHashOps(RedisConst.SECKILL_ORDERS).put(userId,seckillOrder);

                int i = stockCount - seckillOrder.getNum();
                // 更新库存--todo
                goods.setStockCount(i);
                if(i > 0){
                    // 修改redis中商品信息
                    redisTemplate.boundHashOps(RedisConst.SECKILL_GOODS + ":" + time).put(goodsId,goods);
                }else{
                    // 售罄处理，清除redis中商品信息
                    redisTemplate.boundHashOps(RedisConst.SECKILL_GOODS + ":" + time).delete(goodsId);
                    // 同步商品数据
                    secKillGoodsMapper.updateById(goods);
                }


                // 结束
                return;
            }
        }
        // 商品售罄：修改排队状态---3.支付失败
        userRecode.setStatus(3);
        // 更新用户的排队状态
        redisTemplate.boundValueOps(RedisConst.SECKILL_USER + userId).set(userRecode);
        return;

    }

}
